﻿using d1Common;
using d1Common.Interface;
using d1Project.Tools.JsonParse;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace d1Project.Modules.Common.doConfig.Services
{
    public abstract class doConfigServiceBase
    {
        protected Dictionary<string, doJsonNode> cacheModuleJsonNode = new Dictionary<string, doJsonNode>();
        protected Dictionary<string, doJsonValue> cacheConfigValue = new Dictionary<string, doJsonValue>();

        private doJsonValue getConfigJsonValue(string _module, string _group, string _key)
        {
            string _uniqueKey;
            if (_module == null) _module = "";
            if (_module.Length <= 0)
            {
                _uniqueKey = _group + ":" + _key;
            }
            else
            {
                _uniqueKey = _module + ":" + _group + ":" + _key;
            }
            if (!this.cacheConfigValue.ContainsKey(_uniqueKey))
            {
                if (!this.cacheModuleJsonNode.ContainsKey(_module))
                {
                    string configContent = this.readConfigFile(_module, "config.json");
                    if (configContent == null) return null;
                    doJsonValue _rootJsonValue = new doJsonValue();
                    _rootJsonValue.LoadDataFromText(configContent);
                    doJsonNode _jsonNode = _rootJsonValue.GetNode();
                    this.cacheModuleJsonNode[_module] = _jsonNode;
                }

                doJsonNode _jsonModelJsonNode = this.cacheModuleJsonNode[_module];
                if (_jsonModelJsonNode == null) return null;
                doJsonNode _groupNode = _jsonModelJsonNode.GetOneNode(_group);
                if (_groupNode == null) return null;
                doJsonValue _value = _groupNode.GetOneValue(_key);
                if (_value == null) return null;
                this.cacheConfigValue[_uniqueKey] = _value;
            }            
            return this.cacheConfigValue[_uniqueKey];
        }

        public string readConfigStringValue(string _module, string _group, string _key, string _defaultValue)
        {
            doJsonValue _value = this.getConfigJsonValue(_module, _group, _key);
            if (_value == null) return _defaultValue;
            return _value.GetText(_defaultValue);
        }

        public int readConfigIntegerValue(string _module, string _group, string _key, int _defaultValue)
        {
            doJsonValue _value = this.getConfigJsonValue(_module, _group, _key);
            if (_value == null) return _defaultValue;
            return _value.GetInteger(_defaultValue);
        }

        public bool readConfigBoolValue(string _module, string _group, string _key, bool _defaultValue)
        {
            doJsonValue _value = this.getConfigJsonValue(_module, _group, _key);
            if (_value == null) return _defaultValue;
            return _value.GetBoolean(_defaultValue);
        }
        

        public abstract string readConfigFile(string _module, string _path);

        private Dictionary<string, string> localConfigs = new Dictionary<string, string>();
        public string readLocalConfig(string _key, string _defaultValue)
        {
            if (!this.localConfigs.ContainsKey(_key))
            {
                this.localConfigs[_key] = ConfigurationManager.AppSettings[_key] ?? null;
            }
            string _value = this.localConfigs[_key];
            return _value == null ? _defaultValue : _value;
        }

    }
}